package com.beemans.corekit.ui.activity;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.graphics.Insets;
import androidx.core.view.OnApplyWindowInsetsListener;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;

import com.beemans.corekit.constant.AdvertPage;
import com.beemans.corekit.databinding.ActivityAdmobBinding;
import com.beemans.corekit.integration.vpages.GenericFragmentAdapter;
import com.beemans.corekit.integration.vpages.MultiFragmentCreator;
import com.beemans.corekit.ui.fragment.AdmobBannerFragment;
import com.beemans.corekit.ui.fragment.AdmobInsertFragment;
import com.beemans.corekit.ui.fragment.AdmobNativeFragment;
import com.beemans.corekit.ui.fragment.AdmobRewardFragment;
import com.beemans.corekit.ui.fragment.AdmobSplashFragment;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.List;

public class AdmobActivity extends BaseActivity<ActivityAdmobBinding> {
    @Nullable
    @Override
    protected ActivityAdmobBinding getViewBinding() {
        return ActivityAdmobBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initViews() {
        setupStatusBar();
        setupPager();
        bindTabLayout();
    }

    private void setupStatusBar() {
        ViewCompat.setOnApplyWindowInsetsListener(binding.mainViewStatusBar, new OnApplyWindowInsetsListener() {
            @NonNull
            @Override
            public WindowInsetsCompat onApplyWindowInsets(@NonNull View v, @NonNull WindowInsetsCompat insets) {
                final Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
                v.getLayoutParams().height = systemBars.top;
                v.requestLayout();
                return insets;
            }
        });
    }

    private void setupPager() {
        final List<AdvertPage> pages = List.of(AdvertPage.values());
        final GenericFragmentAdapter<AdvertPage> fragmentAdapter = new GenericFragmentAdapter<>(
                this,
                pages,
                new MultiFragmentCreator<>() {
                    @NonNull
                    @Override
                    public Fragment createFragment(@NonNull AdvertPage item, int position) {
                        return switch (item) {
                            case NATIVE -> new AdmobNativeFragment();
                            case BANNER -> new AdmobBannerFragment();
                            case SPLASH -> new AdmobSplashFragment();
                            case INSERT -> new AdmobInsertFragment();
                            case REWARD -> new AdmobRewardFragment();
                        };
                    }
                },
                Enum::ordinal,
                null
        );

        binding.admobVp2.setAdapter(fragmentAdapter);
        binding.admobVp2.setUserInputEnabled(false);
        binding.admobVp2.setOffscreenPageLimit(fragmentAdapter.getItemCount());
    }

    private void bindTabLayout() {
        new TabLayoutMediator(
                binding.admobTabLayout,
                binding.admobVp2,
                false,
                false,
                (tab, position) -> {
                    switch (position) {
                        case 0:
                            tab.setText("Native");
                            break;
                        case 1:
                            tab.setText("Banner");
                            break;
                        case 2:
                            tab.setText("Splash");
                            break;
                        case 3:
                            tab.setText("Insert");
                            break;
                        case 4:
                            tab.setText("Reward");
                            break;
                    }
                }
        ).attach();
    }
}